package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.dto.ReadBehaviorDto;
import com.heima.behavior.entity.ApArticle;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApReadBehavior;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.feign.ApArticleController;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApReadBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import lombok.EqualsAndHashCode;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP阅读行为表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-18
 */
@Service
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements IApReadBehaviorService {

    @Autowired
    IApBehaviorEntryService entryService;

    @Autowired
    private ApArticleController apArticleFeign;

    @Override
    public ResponseResult saveRead(ReadBehaviorDto dto) {

        if (dto == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        User user = AppThreadLocalUtil.get();

        Integer userId = user.getUserId();

        Integer entryId = null;

        if (userId != 0) {

            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId, userId.toString()).eq(ApBehaviorEntry::getType, 1);

            ApBehaviorEntry one = entryService.getOne(query);

            if (one == null) {

                entryId = saveApBehaviorEntey(userId, 1);

                saveReadBehavior(entryId, dto);
            } else {

                saveOrUpdateApBehaviorEntry(one.getId(), dto);
            }
        } else {

            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId, dto.getEquipmentId()).eq(ApBehaviorEntry::getType, 0);

            ApBehaviorEntry one = entryService.getOne(query);

            if (one == null) {

                entryId = saveApBehaviorEntey(Integer.parseInt(dto.getEquipmentId()), 0);

                saveReadBehavior(entryId, dto);
            } else {

                saveOrUpdateApBehaviorEntry(one.getId(), dto);
            }
        }
        ApArticle apArticle = apArticleFeign.selectArticleById(dto.getArticleId());

        /*int likes = apArticle.getLikes();

        Integer i = ++likes;

        apArticle.setLikes(i);*/

        int views = apArticle.getViews();

        Integer integer1 = ++views;

        apArticle.setViews(integer1);

        ResponseResult responseResult = apArticleFeign.selectArticleById(apArticle);

        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {

            return ResponseResult.okResult();
        }
        return null;
    }

    private void saveOrUpdateApBehaviorEntry(Integer entryId, ReadBehaviorDto dto) {

        LambdaQueryWrapper<ApReadBehavior> query = new LambdaQueryWrapper<>();

        query.eq(ApReadBehavior::getEntryId, entryId).eq(ApReadBehavior::getArticleId, dto.getArticleId());

        ApReadBehavior one = this.getOne(query);

        if (one == null) {

            one = new ApReadBehavior();

            one.setArticleId(dto.getArticleId());

            one.setPercentage(dto.getPercentage());

            one.setLoadDuration(dto.getLoadDuration());

            one.setCount(dto.getCount());

            one.setEntryId(entryId);

            one.setReadDuration(dto.getReadDuration());

            one.setCreatedTime(new Date());

            one.setUpdatedTime(new Date());

            this.save(one);
        } else {

            one.setArticleId(dto.getArticleId());

            one.setPercentage(dto.getPercentage());

            one.setLoadDuration(dto.getLoadDuration());

            one.setCount(dto.getCount());

            one.setEntryId(entryId);

            one.setReadDuration(dto.getReadDuration());

            one.setCreatedTime(new Date());

            one.setUpdatedTime(new Date());

            this.updateById(one);
        }
    }

    private void saveReadBehavior(Integer entryId, ReadBehaviorDto dto) {

        ApReadBehavior behavior = new ApReadBehavior();

        behavior.setUpdatedTime(new Date());

        behavior.setCreatedTime(new Date());

        behavior.setEntryId(entryId);

        behavior.setArticleId(dto.getArticleId());

        behavior.setCount(dto.getCount());

        behavior.setLoadDuration(dto.getLoadDuration());

        behavior.setPercentage(dto.getPercentage());

        behavior.setArticleId(dto.getArticleId());

        this.save(behavior);
    }

    private Integer saveApBehaviorEntey(Integer userId, Integer type) {

        ApBehaviorEntry entry = new ApBehaviorEntry();

        entry.setEntryId(userId.toString());

        entry.setType(type);

        entry.setCreatedTime(new Date());

        entryService.save(entry);

        return entry.getId();
    }

   /* @Override
    public ResponseResult saveRead(ReadBehaviorDto com.heima.search.dto) {

        if (com.heima.search.dto == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        User user = AppThreadLocalUtil.get();

        Integer userId = user.getUserId();

        //  ApLikesBehavior apLikesBehavior = new ApLikesBehavior();

        ApReadBehavior apLikesBehavior = new ApReadBehavior();

        if (userId != 0) {

            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId,userId.toString());

            query.eq(ApBehaviorEntry::getType,1);

            ApBehaviorEntry one = entryService.getOne(query);

            if(one==null){

                one = new ApBehaviorEntry();

                one.setCreatedTime(new Date());

                one.setType(1);

                one.setEntryId(userId.toString());

                entryService.save(one);
            }
            apLikesBehavior.setEntryId(one.getId());
        }else {
            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId,com.heima.search.dto.getEquipmentId());

            //   String equipmentId = com.heima.search.dto.getEquipmentId();

            query.eq(ApBehaviorEntry::getType,0);

            ApBehaviorEntry one = entryService.getOne(query);

            if(one==null){

                one = new ApBehaviorEntry();

                one.setCreatedTime(new Date());

                one.setType(0);

                one.setEntryId(com.heima.search.dto.getEquipmentId());

                entryService.save(one);
            }
            apLikesBehavior.setEntryId(one.getId());
        }
        apLikesBehavior.setCreatedTime(new Date());

        // apLikesBehavior.setOperation(com.heima.search.dto.getOperation());
        //apLikesBehavior.setType(com.heima.search.dto.getType());
       // apLikesBehavior.set

        apLikesBehavior.setArticleId(com.heima.search.dto.getArticleId());

        this.save(apLikesBehavior);

        return ResponseResult.okResult(apLikesBehavior.getId());

        *//*if (com.heima.search.dto == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApReadBehavior apReadBehavior = new ApReadBehavior();

        BeanUtils.copyProperties(com.heima.search.dto, apReadBehavior);

        ApBehaviorEntry one = null;

        if (com.heima.search.dto.getUserId() != null) {

            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId, com.heima.search.dto.getUserId());

            query.eq(ApBehaviorEntry::getType, 1);

            one = entryService.getOne(query);
        }
        if (com.heima.search.dto.getEquipmentId() != null) {

            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId, com.heima.search.dto.getUserId());

            query.eq(ApBehaviorEntry::getType, 0);

            one = entryService.getOne(query);
        }

        apReadBehavior.setEntryId(one.getId());

        LambdaQueryWrapper<ApReadBehavior> query = new LambdaQueryWrapper<>();

        query.eq(ApReadBehavior::getArticleId, com.heima.search.dto.getArticleId());

        query.eq(ApReadBehavior::getEntryId, one.getId());

        ApReadBehavior one1 = this.getOne(query);

        if (one1 == null) {

            apReadBehavior.setCreatedTime(new Date());
        }
        apReadBehavior.setUpdatedTime(new Date());

        if (one1 == null) {

            this.save(apReadBehavior);
        } else {

            this.updateById(apReadBehavior);
        }
        return ResponseResult.okResult();*//*
    }*/
}
